Fletcher Checksum
The checksum is a 2 byte Fletcher checksum and encompasses all the bytes in the packet (excluding the packet checksum bytes)
Fletcher Checksum algorithm
uint16_t fletcher_checksum(const uint8_t* packet, int packet_length)
{
// Checksum covers from the first header byte to the last payload byte.
// This should be equal to the payload length plus the 4 header bytes.
const int checksum_length = packet_length - 2;
uint8_t checksum_MSB = 0;
uint8_t checksum_LSB = 0;
// Iterate over the packet to compute the checksum.
for(int i=0; i<checksum_length; i++)
{
checksum_MSB += packet[i];
checksum_LSB += checksum_MSB;
}
return ((uint16_t)checksum_MSB << 8) | (uint16_t)checksum_LSB;
}